package srv

import (
	"fmt"
	"jgt-wallet/src/bll"
	"jgt-wallet/src/context"
	"jgt-wallet/src/schema"

	"github.com/spf13/viper"
)

// Demo 示例接口
type WechatApp struct {
	WechatAppBll *bll.WechatApp `inject:""`
	OrderBll     *bll.Order     `inject:""`
}

// 支付宝应用支付接口
func (a *WechatApp) Pay(ctx *context.Context) error {
	var (
		key    string
		param  = new(schema.WechatAppPayParams)
		err    error
		result map[string]string
	)
	err = ctx.ParseJSON(param)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}

	key = ctx.Param("key")
	param.Url = fmt.Sprintf("%s/api/v1/public/wechat/callback/app/%s", viper.GetString("web_addr"), key)
	result, err = a.WechatAppBll.Pay(key, param)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	return ctx.ResSuccess(result)
}

// 微信支付同步回调接口
func (a *WechatApp) Check(ctx *context.Context) error {
	var (
		key     string
		orderId string
		result  *schema.WechatAppCheckResult
		err     error
	)
	orderId = ctx.Param("orderId")
	key = ctx.Param("key")
	result, err = a.WechatAppBll.Check(key, orderId)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}

	return ctx.ResSuccess(result)
}

// 检查订单支付结果
func (a *WechatApp) CheckResult(ctx *context.Context) error {
	return ctx.ResOK()
}
